<%@ page language="java" contentType="application/x-javascript; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="/commons/pages/taglibs.jsp" %>
<c:if test="${false}">
<script type="text/javascript">
//</c:if>
////////////////////////////////////////////////////////////////////////////////
//JQUERY EXTENTION
////////////////////////////////////////////////////////////////////////////////
var ctx = '${ctx}';

$.fn.serializeObject = function() {  
    var o = {};  
    var a = this.serializeArray();  
    $.each(a, function() {  
        if (o[this.name]) {  
            if (!o[this.name].push) {  
                o[this.name] = [ o[this.name] ];  
            }  
            o[this.name].push(this.value || '');  
        } else {  
            o[this.name] = this.value || '';  
        }  
    });  
    return o;  
};
////////////////////////////////////////////////////////////////////////////////
// INITALIZATIONS
////////////////////////////////////////////////////////////////////////////////
function init() {
	$("select[id='is_del']").each(function(i){ //是否删除
		$(this).combobox({
			panelHeight: "auto",
			editable: false,
			url: ctx + '/platform/type-info/json?type_sign=BOOLEAN',
		    valueField: 'type_value',  
		    textField: 'type_name',
		    value: 0
		});
	});
	
	$("select[id='is_lock'], select[id='is_active']").each(function(i){//是否锁定，是否激活
		$(this).combobox({
			panelHeight: "auto",
			editable: false,
			url: ctx + '/platform/type-info/json?type_sign=BOOLEAN',
		    valueField: 'type_value',  
		    textField: 'type_name'
		    //value: i % 2 == 0 ? '' : 0
		});
	});
	
	$("select[id='gender']").each(function(i){//性别
		$(this).combobox({
			panelHeight: "auto",
			editable: false,
			url: ctx + '/platform/type-info/json?type_sign=GENDER',
		    valueField: 'type_value',
		    textField: 'type_name'
		});
	});
	
	$("select[id='customer_type']").each(function(i){//客户类别
		$(this).combobox({
			panelHeight: "auto",
			editable: false,
			url: ctx + '/platform/type-info/json?type_sign=CUSTOMER',
		    valueField: 'type_value',
		    textField: 'type_name'
		});
	});
	
	
	$("select[id='modem_id']").each(function(i){//DTU_ID
		$(this).combobox({
			panelHeight: "auto",
			editable: false,
			url: ctx + '/platform/user-dtu/json',
		    valueField: 'modem_id',
		    textField: 'modem_id'
		});
	});
	
	if ($('#dg')) {//datagrid随窗口变化大小
		$(window).resize(function() {
			$('#dg').datagrid('resize');
		});
	}
}

$(function(){
	init();
});

//JSON.stringify($('#fm').serializeObject()),
////////////////////////////////////////////////////////////////////////////////
// ACTIONS
////////////////////////////////////////////////////////////////////////////////
var msg_title_info = '提示';
var msg_title_confirm = '确认';
var msg_title_warning = '警告';
var msg_title_error = '错误';

function doSearch(cfg){
	var cfg = cfg || {};
	var grid = cfg.grid || 'datagrid';
	var url = cfg.url || (grid == 'treegrid' ? $('#dg').treegrid('options').url : $('#dg').datagrid('options').url);
	var title = cfg.title || '搜索';
	
	if (grid == 'treegrid'){
		$.post(url, $('#fm_search').serializeObject(), function(data){
		 	$('#dg').treegrid('loadData', data);
		}, 'json');
	} else {
		$('#dg').datagrid('load', $('#fm_search').serializeObject()); 
	}
}

function doSave(cfg){
	var cfg = cfg || {};
	var grid = cfg.grid || 'datagrid';
	var url = cfg.url || (grid == 'treegrid' ? $('#dg').treegrid('options').url : $('#dg').datagrid('options').url);
	var title = cfg.title || '保存';
	var oper  = cfg.oper || 'confirm';
	
	url = url.replace('search', 'save');
	
	$('#fm').form('submit', {
		url: url,
	    onSubmit: function(){  
	        return $(this).form('validate');
	    },  
	    success: function(result){
	    	var result = eval('(' + result + ')');
			if (result.success){
				switch (oper) {
					case 'confirm':
						if ($('#id').val()) {
							$.messager.show({title:msg_title_info, timeout:3000, msg:'保存数据成功!'});
							$('#dlg').dialog('close');
						} else {
							$.messager.confirm(msg_title_confirm, '保存成功，是否继续添加数据？',function(r){
								if (!r){
									$('#dlg').dialog('close');
								}
							});
						}
						break;
					case 'tip':
						$.messager.show({title:msg_title_info, timeout:3000, msg:result.msg});
						break;
					case 'login-redirect':
						parent.location.href = ctx + '/platform/main';
						break;
				}
				doSearch({url: url.replace('save', 'search'), grid:grid});
			} else {
			    $.messager.show({title:msg_title_info, timeout:3000, msg:result.msg});
			}
	    }
	});
}
function doAdd(cfg){
	var cfg = cfg || {};
	var grid = cfg.grid || 'datagrid';
	var url = cfg.url || (grid == 'treegrid' ? $('#dg').treegrid('options').url : $('#dg').datagrid('options').url);
	var title = cfg.title || '添加';
	
	$('#dlg').dialog('open').dialog('setTitle', title);
	$('#fm').form('clear');
	//$('#fm').find('#id').val("");
}
function doEdit(cfg) {
	var cfg = cfg || {};
	var grid = cfg.grid || 'datagrid';
	var url = cfg.url || (grid == 'treegrid' ? $('#dg').treegrid('options').url : $('#dg').datagrid('options').url);
	var title = cfg.title || '修改';
	
	url = url.replace('search', 'edit/');

	var rows = (grid == 'treegrid' ? $('#dg').treegrid('getSelections') : $('#dg').datagrid('getSelections'));
	if (rows.length != 1) {
		$.messager.alert(msg_title_info, '只能选择一条记录修改', 'warning');
		return;
	}
	$('#dlg').dialog('open').dialog('setTitle', title);
	$('#fm').form('clear');
	$('#fm').form({
    	onLoadSuccess:function(data){
    		if (data && data.role_codes) {//用户之角色代码
    			$('#role_codes').combobox('clear').combobox('setValues', data.role_codes.split(','));
    		}
		}
	}).form('load', url + rows[0].id);
	//$('#fm').form('load', url + rows[0].id);
}
function doRemove(cfg){
	var cfg = cfg || {};
	var grid = cfg.grid || 'datagrid';
	var url = cfg.url || (grid == 'treegrid' ? $('#dg').treegrid('options').url : $('#dg').datagrid('options').url);
	var title = cfg.title || '删除';
	
	url = url.replace('search', 'delete');
	
	var rows = (grid == 'treegrid' ? $('#dg').treegrid('getSelections') : $('#dg').datagrid('getSelections'));
	if (rows.length < 1) {
		$.messager.alert(msg_title_info, '至少选择一条记录！', 'warning');
		return;
	}
	$.messager.confirm(msg_title_confirm, '确认删除选中的所有的记录吗？',function(r){  
		if (r){
			var ids = [];  
			for(var i = 0; i < rows.length; i++){  
				ids.push(rows[i].id);  
			}
			$.getJSON(url, { ids: ids.join(','), rnd: Math.random() }, function (data){
				if (data.success){
					$.messager.show({title:msg_title_info, timeout:3000, msg:'成功删除' + data.count + '条记录!'});
				}else if (data.error){
					$.messager.show({title:msg_title_info, timeout:3000, msg:'删除失败!<br />' + data.msg});
				}
				doSearch({url: url.replace('delete', 'search'), grid:grid});
			});
		}
	});
}

//<c:if test="${false}">
</script>
</c:if>